## Replication code for Estimating Candidates’ Political Orientation in a Polarized Congress
## We use R for 3.2.4 GUI 1.67 Mavericks build (7152), running on Mac OS 10.12.1 

library(foreign) ## We use version: foreign_0.8-66
library(ggplot2) ## We use version: ggplot2_2.1.0
library(plyr)    ## We use version: plyr_1.8.4
library(dplyr)   ## We use version: dplyr_0.5.0
library(doBy)    ## We use version: doBy_4.5-14
library(splines) ## Base R
library(MASS)	 ## We use version: MASS_7.3-45


load(file=paste("candidatepositions_house_analysis.RData",sep=""))

quantile.classify<-function(validation3 , q2, q3, q4, metric, benchmark){
	#benchmark=dwnom1_contemporaneous
	#metric=cfscores.dyn
	validation3<-cbind(validation3, metric)
	validation3<-cbind(validation3, benchmark)
	validation3<-subset(validation3, !is.na(benchmark))
	validation3<-subset(validation3, !is.na(metric))
	reg<-lm(benchmark~metric , data=validation3)
	prediction<-predict(reg)
	validation3<-cbind(validation3, prediction)
	absolute_error<-abs(prediction-validation3$metric)

	validation3$q2[ !is.na(validation3$benchmark)] <- cut(validation3$benchmark[  !is.na(validation3$benchmark)], q2, include.lowest=TRUE,labels=paste("Group", 1:2))
	validation3$q3[!is.na(validation3$benchmark)] <- cut(validation3$benchmark[!is.na(validation3$benchmark)], q3, include.lowest=TRUE,labels=paste("Group", 1:3))
	validation3$q4[!is.na(validation3$benchmark)] <- cut(validation3$benchmark[!is.na(validation3$benchmark)], q4, include.lowest=TRUE,labels=paste("Group", 1:4))  
       
	validation3$q2_predict[ !is.na(validation3$benchmark)] <- cut(validation3$prediction[ !is.na(validation3$benchmark)], q2, include.lowest=TRUE,labels=paste("Group", 1:2))
	validation3$q3_predict[ !is.na(validation3$benchmark)] <- cut(validation3$prediction[ !is.na(validation3$benchmark)], q3, include.lowest=TRUE,labels=paste("Group", 1:3))
	validation3$q4_predict[ !is.na(validation3$benchmark)] <- cut(validation3$prediction[!is.na(validation3$benchmark)], q4, include.lowest=TRUE,labels=paste("Group", 1:4)) 
	return(validation3)
}

## Baseline correlation between Nokken-Poole and DW-Nominate
#cor(analysis_all$nokken_d1[analysis_all$Party == 100],
#    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 100], use="complete.obs")

#cor(analysis_all$nokken_d1[analysis_all$Party == 200],
#    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 200], use="complete.obs")

## Correlation between AM-Estimate and DW-Nominate
cor(analysis_all$am_estimate_tw[analysis_all$Party == 100],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 100], use="complete.obs")

cor(analysis_all$am_estimate_tw[analysis_all$Party == 200],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 200], use="complete.obs")

## Correlation between CF-Score and DW-Nominate
cor(analysis_all$cfscore[analysis_all$Party == 100],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 100], use="complete.obs")

cor(analysis_all$cfscores.dyn[analysis_all$Party == 200],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 200], use="complete.obs")


# Correlation between CF-Score and DW-Nominate
cor(analysis_all$cfscore[analysis_all$Party == 100],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 100], use="complete.obs")

cor(analysis_all$cfscores.dyn[analysis_all$Party == 200],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 200], use="complete.obs")


## Correlation between Twitter ideal points and DW-Nominate
cor(analysis_all$twitter_idealpoint[analysis_all$Party == 100],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 100], use="complete.obs")

cor(analysis_all$twitter_idealpoint[analysis_all$Party == 200],
    analysis_all$dwnom1_contemporaneous[analysis_all$Party == 200], use="complete.obs")


#######################
### Make Graphs
#######################


# Multiple plot function
multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL) {
  require(grid)

                                        # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

                                        # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
                                        # Make the panel
                                        # ncol: Number of columns of plots
                                        # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), 
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }

  if (numPlots == 1) {
    print(plots[[1]])

  } else {
                                        # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

                                        # Make each plot, in the correct location
    for (i in 1:numPlots) {
                                        # Get the i, j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout ==  i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, 
                            layout.pos.col = matchidx$col))
    }
  }
}

analysis_2000s<-subset(analysis_all, election>1998)

q2_dems <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==100 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.5,1), na.rm=T)
q3_dems <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==100 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.33,.66,1), na.rm=T)
q4_dems <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==100 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.25, .5,.75,1), na.rm=T)


q2_reps <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==200 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.5,1), na.rm=T)
q3_reps <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==200 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.33,.66,1), na.rm=T)
q4_reps <- quantile(analysis_2000s$dwnom1_contemporaneous[analysis_2000s$Party==200 & !is.na(analysis_2000s$dwnom1_contemporaneous)], probs = c(0, 0.25, .5,.75,1), na.rm=T)

###############
## Figure 1: Contemporaneous validity
###############


###############
## Compare DW Nominate and CF-Score estimates
###############

###
## Calculate ability of model to correctly classify candidates into correct party
###
validation3 <- subset(analysis_2000s , !is.na(Party) & !is.na(dwnom1_contemporaneous))
validation3 <- as.data.frame(validation3)

validation3$Party2<-as.numeric(factor(validation3$Party))-1
reg<-glm(Party2~cfscores.dyn, data=validation3, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & validation3$Party2==1]<-1
correct[predictions<.5 & validation3$Party2==0]<-1
correct[predictions<.5 & validation3$Party2==1]<-0
correct[predictions>.5 & validation3$Party2==0]<-0
cf_party_contemporaneous<-table(correct)[2]/length(correct)
cf_observations_contemporaneous<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###

validation3 <- subset(analysis_2000s, Party == 100 )
validation3 <- as.data.frame(validation3)
validation3$dwnom1_contemporaneous<-as.numeric(as.vector(validation3$dwnom1_contemporaneous))
cors_cf_contemporaneous_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, cfscores.dyn,use = "complete.obs"), 2))

cfscores.dyn<-validation3$cfscores.dyn
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=cfscores.dyn, benchmark=dwnom1_contemporaneous)

q2_correct_cf_contemperaneous_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_contemperaneous_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_contemperaneous_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_contemperaneous_dems<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_cf_dems<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_cf_contemperaneous_dems_apre<-(mae_baseline_cf_dems-mae_cf_contemperaneous_dems)/mae_baseline_cf_dems


cfscores_dems <- (ggplot(data = validation3, aes(x = cfscores.dyn, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
      + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(-2, 1))
       +scale_y_continuous(limits=c(-.75,.25))
     + geom_text(data = cors_cf_contemporaneous_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = .5, y =-.58)
          +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.y =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(analysis_2000s, Party == 200 )
validation3 <- as.data.frame(validation3)
validation3$dwnom1_contemporaneous<-as.numeric(as.vector(validation3$dwnom1_contemporaneous))

cors_cf_contemporaneous_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      cfscores.dyn,use = "complete.obs"), 2))
                                                      
                                                      cfscores.dyn<-validation3$cfscores.dyn
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=cfscores.dyn, benchmark=dwnom1_contemporaneous)

q2_correct_cf_contemperaneous_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_contemperaneous_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_contemperaneous_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_contemperaneous_reps<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_cf_reps<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_cf_contemperaneous_reps_apre<-(mae_baseline_cf_reps-mae_cf_contemperaneous_reps)/mae_baseline_cf_reps


cfscores_reps <- (ggplot(data = validation3, aes(x = cfscores.dyn, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(0, 2))
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_cf_contemporaneous_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.5, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )





###############
## Compare DW Nominate and Twitter estimates
###############
###
## Calculate ability of model to correctly classify candidates into correct party
###
validation3 <- subset(analysis_2000s , !is.na(Party) & !is.na(twitter_idealpoint) & election==2012)
validation3 <- as.data.frame(validation3)

validation3$Party2<-as.numeric(factor(validation3$Party))-1
reg<-glm(Party2~twitter_idealpoint, data=validation3, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & validation3$Party2==1]<-1
correct[predictions<.5 & validation3$Party2==0]<-1
correct[predictions<.5 & validation3$Party2==1]<-0
correct[predictions>.5 & validation3$Party2==0]<-0
twitter_party_contemporaneous<-table(correct)[2]/length(correct)
twitter_observations_contemporaneous<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###

twitter.collapsed <- summaryBy( dwnom1_contemporaneous +twitter_idealpoint
                            ~ICPSR2+Party +name, FUN=c(mean),na.rm=T, data=validation3)
colnames(twitter.collapsed)<-gsub( ".mean", "",colnames(twitter.collapsed))
 twitter.collapsed<-subset(  twitter.collapsed, !is.na(  twitter_idealpoint))                                      
 twitter.collapsed$dwnom1_contemporaneous<- as.numeric(as.vector(twitter.collapsed$dwnom1_contemporaneous))
validation3 <- subset(twitter.collapsed, Party == 100 )
validation3 <- as.data.frame(validation3)

cors_twitter_contemporaneous_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      twitter_idealpoint,use = "complete.obs"), 2))
                                                      
twitter_idealpoint<-validation3$twitter_idealpoint
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=twitter_idealpoint, benchmark=dwnom1_contemporaneous)

q2_correct_twitter_contemperaneous_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_twitter_contemperaneous_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_twitter_contemperaneous_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_twitter_contemperaneous_dems<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_twitter_dems<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_twitter_contemperaneous_dems_apre<-(mae_baseline_twitter_dems-mae_twitter_contemperaneous_dems)/mae_baseline_twitter_dems


twitter_dems <- (ggplot(data = validation3, aes(x = twitter_idealpoint, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 1,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Twitter Score (112th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,.25))
      + geom_text(data = cors_twitter_contemporaneous_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.6, y =-.58)
          +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(twitter.collapsed, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_twitter_contemporaneous_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      twitter_idealpoint,use = "complete.obs"), 2))

twitter_idealpoint<-validation3$twitter_idealpoint
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=twitter_idealpoint, benchmark=dwnom1_contemporaneous)

q2_correct_twitter_contemperaneous_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_twitter_contemperaneous_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_twitter_contemperaneous_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_twitter_contemperaneous_reps<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_twitter_reps<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_twitter_contemperaneous_reps_apre<-(mae_baseline_twitter_reps-mae_twitter_contemperaneous_reps)/mae_baseline_twitter_reps


twitter_reps <- (ggplot(data = validation3, aes(x = twitter_idealpoint, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 1,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Twitter Score (112th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_twitter_contemporaneous_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.6, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )



###############
## Compare DW Nominate and A-M estimates
###############

bonica_2010<-subset(analysis_2000s, election==2010)
bonica_2010$Party<-as.vector(bonica_2010$Party)
bonica_2010$dwnom1_contemporaneous<-as.numeric(as.vector(bonica_2010$dwnom1_contemporaneous))
bonica_2010$am_estimate_tw<-as.numeric(as.vector(bonica_2010$am_estimate_tw))

###
## Calculate ability of model to correctly classify candidates into correct party
###
validation3 <- subset(bonica_2010 , !is.na(Party) & !is.na(am_estimate_tw))
validation3 <- as.data.frame(validation3)

validation3$Party2<-as.numeric(factor(validation3$Party))-1
reg<-glm(Party2~am_estimate_tw, data=validation3, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & validation3$Party2==1]<-1
correct[predictions<.5 & validation3$Party2==0]<-1
correct[predictions<.5 & validation3$Party2==1]<-0
correct[predictions>.5 & validation3$Party2==0]<-0
## all correct
am_party_contemporaneous<-table(correct)[1]/length(correct)
am_observations_contemporaneous<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###


validation3 <- subset(bonica_2010, Party == 100 )
validation3 <- as.data.frame(validation3)

cors_am_contemporaneous_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      am_estimate_tw,use = "complete.obs"), 2))
                                                      
                                                                                                            
am_estimate_tw<-validation3$am_estimate_tw
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=am_estimate_tw, benchmark=dwnom1_contemporaneous)

q2_correct_am_contemperaneous_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_am_contemperaneous_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_am_contemperaneous_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_am_contemperaneous_dems<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_am_dems<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_am_contemperaneous_dems_apre<-(mae_baseline_am_dems-mae_am_contemperaneous_dems)/mae_baseline_am_dems


am_dems <- (ggplot(data = validation3, aes(x = am_estimate_tw, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
	+ stat_smooth(method = "lm", formula = y ~ ns(x,2), se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Aldrich-McKelvey Score (112th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,.25))
      + geom_text(data = cors_am_contemporaneous_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.35, y =-.58)
          +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(bonica_2010, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_am_contemporaneous_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      am_estimate_tw,use = "complete.obs"), 2))
                                                      
                                                      am_estimate_tw<-validation3$am_estimate_tw
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=am_estimate_tw, benchmark=dwnom1_contemporaneous)

q2_correct_am_contemperaneous_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_am_contemperaneous_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_am_contemperaneous_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_am_contemperaneous_reps<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_am_reps<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_am_contemperaneous_reps_apre<-(mae_baseline_am_reps-mae_am_contemperaneous_reps)/mae_baseline_am_reps
am_reps <- (ggplot(data = validation3, aes(x = am_estimate_tw, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
	+ stat_smooth(method = "lm", formula = y ~ ns(x,2), se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Aldrich-McKelvey Score (112th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_am_contemporaneous_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.25, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )



###############
## Compare DW Nominate and J-S estimates
###############
###
## Calculate ability of model to correctly classify candidates into correct party
###
validation3 <- subset(analysis_2000s , !is.na(Party) & !is.na(js_ideology_contemporaneous))
validation3 <- as.data.frame(validation3)

validation3$Party2<-as.numeric(factor(validation3$Party))-1
reg<-glm(Party2~js_ideology_contemporaneous, data=validation3, family="binomial")
predictions<-predict(reg, type="response")
correct<-rep(NA, length=length(predictions))
correct[predictions>.5 & validation3$Party2==1]<-1
correct[predictions<.5 & validation3$Party2==0]<-1
correct[predictions<.5 & validation3$Party2==1]<-0
correct[predictions>.5 & validation3$Party2==0]<-0
## all correct, so we use the first column from table
js_party_contemporaneous<-table(correct)[1]/length(correct)
js_observations_contemporaneous<-length(predictions)



###
## Calculate ability of model to predict candidate ideology
###
validation3 <- subset(analysis_2000s, Party == 100  & !is.na(js_ideology_contemporaneous))
validation3 <- as.data.frame(validation3)

cors_js_contemporaneous_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      js_ideology_contemporaneous,use = "complete.obs"), 3))
                                                      
js_ideology_contemporaneous<-validation3$js_ideology_contemporaneous   
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=js_ideology_contemporaneous, benchmark=dwnom1_contemporaneous)

q2_correct_js_contemperaneous_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_js_contemperaneous_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_js_contemperaneous_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_js_contemperaneous_dems<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_js_dems<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_js_contemperaneous_dems_apre<-(mae_baseline_js_dems-mae_js_contemperaneous_dems)/mae_baseline_js_dems

js_dems <- (ggplot(data = validation3, aes(x = js_ideology_contemporaneous, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Expert Assessment Score (111th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,.25))
      + geom_text(data = cors_js_contemporaneous_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, 
      #                                               x= 2, y =.15)
                                                      x= 4, y =-.58)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(analysis_2000s, Party == 200 & !is.na(js_ideology_contemporaneous))
validation3 <- as.data.frame(validation3)

cors_js_contemporaneous_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_contemporaneous, 
                                                      js_ideology_contemporaneous,use = "complete.obs"), 3))
                                                      
js_ideology_contemporaneous<-validation3$js_ideology_contemporaneous   
dwnom1_contemporaneous<-validation3$dwnom1_contemporaneous
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=js_ideology_contemporaneous, benchmark=dwnom1_contemporaneous)

q2_correct_js_contemperaneous_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_js_contemperaneous_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_js_contemperaneous_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_js_contemperaneous_reps<-mean(abs(validation3$prediction-validation3$dwnom1_contemporaneous))
mae_baseline_js_reps<-mean(abs(mean(validation3$dwnom1_contemporaneous)-validation3$dwnom1_contemporaneous))
mae_js_contemperaneous_reps_apre<-(mae_baseline_js_reps-mae_js_contemperaneous_reps)/mae_baseline_js_reps

js_reps <- (ggplot(data = validation3, aes(x = js_ideology_contemporaneous, y =
                                         dwnom1_contemporaneous))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Expert Assessment Score (111th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
     + geom_text(data = cors_js_contemporaneous_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, 
                                                     x = 6.35, y =.1)
                                             #         x= 4, y =-.58)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )

## ## ## ## 
## Figure 1
## ## ## ## 
pdf(file = paste("Figure1a.pdf", sep = ""),
    width = 8, height = 6)

multiplot( js_dems, twitter_dems,am_dems,cfscores_dems, cols = 2)
dev.off()

pdf(file = paste("Figure1b.pdf", sep = ""),
    width = 8, height = 6)

multiplot( js_reps, twitter_reps,am_reps,cfscores_reps, cols = 2)
dev.off()

###############
### Figure 2- Focus on ability of measures to predict nominate
###############

###############
## Compare DW Nominate and J-S estimates
###############
###
## Calculate ability of model to correctly classify candidates into correct party
###
validation3 <- subset(analysis_2000s , !is.na(Party) & !is.na(js_ideology_prospective))
validation3 <- as.data.frame(validation3)

validation3$Party2<-as.numeric(factor(validation3$Party))-1
reg<-glm(Party2~js_ideology_prospective, data=validation3, family="binomial")
predictions<-predict(reg, type="response")
correct<-rep(NA, length=length(predictions))
correct[predictions>.5 & validation3$Party2==1]<-1
correct[predictions<.5 & validation3$Party2==0]<-1
correct[predictions<.5 & validation3$Party2==1]<-0
correct[predictions>.5 & validation3$Party2==0]<-0
## all correct, so we use the first column from table
js_party_prospective<-table(correct)[1]/length(correct)
js_observations_prospective<-length(predictions)



###
## Calculate ability of model to predict candidate ideology
###
validation3 <- subset(analysis_2000s, Party == 100  & !is.na(js_ideology_prospective))
validation3 <- as.data.frame(validation3)

cors_js_prospective_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      js_ideology_prospective,use = "complete.obs"), 3))
                                                      
js_ideology_prospective<-validation3$js_ideology_prospective   
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=js_ideology_prospective, benchmark=dwnom1_prospective)

q2_correct_js_contemperaneous_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_js_contemperaneous_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_js_contemperaneous_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_js_contemperaneous_dems<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_js_dems<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_js_contemperaneous_dems_apre<-(mae_baseline_js_dems-mae_js_contemperaneous_dems)/mae_baseline_js_dems

js_dems <- (ggplot(data = validation3, aes(x = js_ideology_prospective, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Expert Assessment Score (111th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,.25))
      + geom_text(data = cors_js_prospective_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, 
      #                                               x= 2, y =.15)
                                                      x= 4, y =-.58)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(analysis_2000s, Party == 200 & !is.na(js_ideology_prospective))
validation3 <- as.data.frame(validation3)

cors_js_prospective_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      js_ideology_prospective,use = "complete.obs"), 3))
                                                      
js_ideology_prospective<-validation3$js_ideology_prospective   
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=js_ideology_prospective, benchmark=dwnom1_prospective)

q2_correct_js_contemperaneous_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_js_contemperaneous_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_js_contemperaneous_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_js_contemperaneous_reps<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_js_reps<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_js_contemperaneous_reps_apre<-(mae_baseline_js_reps-mae_js_contemperaneous_reps)/mae_baseline_js_reps

js_reps <- (ggplot(data = validation3, aes(x = js_ideology_prospective, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
      + geom_smooth(method = "loess", se = FALSE, span = 0.8,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Expert Assessment Score (111th Congress)")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
     + geom_text(data = cors_js_prospective_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, 
                                                     x = 6.35, y =.1)
                                             #         x= 4, y =-.58)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )


###############
## Compare DW Nominate and CF-Score estimates
###############

###
## Calculate ability of model to correctly classify candidates into correct party
###
prospective <- subset(analysis_2000s , !is.na(Party) & !is.na(cfscore) & !is.na(dwnom1_prospective))
prospective <- as.data.frame(prospective)

prospective$Party2<-as.numeric(factor(prospective$Party))-1
reg<-glm(Party2~cfscore, data=prospective, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & prospective$Party2==1]<-1
correct[predictions<.5 & prospective$Party2==0]<-1
correct[predictions<.5 & prospective$Party2==1]<-0
correct[predictions>.5 & prospective$Party2==0]<-0
cf_party_predictive<-table(correct)[2]/length(correct)
cf_observations_predictive<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###

validation3 <- subset(prospective, Party == 100 )
validation3 <- as.data.frame(validation3)

cors_cf_predictive_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                     cfscores.dyn,use = "complete.obs"), 2))


cfscores.dyn<-validation3$ cfscores.dyn
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric= cfscores.dyn, benchmark=dwnom1_prospective)

q2_correct_cf_predictive_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_predictive_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_predictive_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_predictive_dems<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_cf_dems<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_cf_predictive_dems_apre<-(mae_baseline_cf_dems-mae_cf_predictive_dems)/mae_baseline_cf_dems



cfscores_dems <- (ggplot(data = validation3, aes(x =  cfscores.dyn, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)

     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(-2, 0))
       +scale_y_continuous(limits=c(-.75,0))
     + geom_text(data = cors_cf_predictive_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.6, y =-.65)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(prospective, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_cf_predictive_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                       cfscores.dyn,use = "complete.obs"), 2))
 cfscores.dyn<-validation3$ cfscores.dyn
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric= cfscores.dyn, benchmark=dwnom1_prospective)

q2_correct_cf_predictive_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_predictive_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_predictive_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_predictive_reps<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_cf_reps<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_cf_predictive_reps_apre<-(mae_baseline_cf_reps-mae_cf_predictive_reps)/mae_baseline_cf_reps


cfscores_reps <- (ggplot(data = validation3, aes(x =  cfscores.dyn, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(0, 1.6))
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_cf_predictive_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.25, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )



###############
## Compare DW Nominate and NPAT-sores estimates
###############

###
## Calculate ability of model to correctly classify candidates into correct party
###
prospective <- subset(analysis_2000s , !is.na(Party) & !is.na(npat_score) & !is.na(dwnom1_prospective))
prospective <- as.data.frame(prospective)

prospective$Party2<-as.numeric(factor(prospective$Party))-1
reg<-glm(Party2~npat_score, data=prospective, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & prospective$Party2==1]<-1
correct[predictions<.5 & prospective$Party2==0]<-1
correct[predictions<.5 & prospective$Party2==1]<-0
correct[predictions>.5 & prospective$Party2==0]<-0
cf_party_predictive<-table(correct)[2]/length(correct)
cf_observations_predictive<-length(predictions)

###
## Calculate ability of model to estimate prospective dw-nominate scores
###

validation3 <- subset(prospective, Party == 100 )
validation3 <- as.data.frame(validation3)

cors_cf_predictive_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                     npat_score,use = "complete.obs"), 2))


npat_score<-validation3$ npat_score
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric= npat_score, benchmark=dwnom1_prospective)

q2_correct_cf_predictive_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_predictive_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_predictive_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_predictive_dems<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_cf_dems<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_cf_predictive_dems_apre<-(mae_baseline_cf_dems-mae_cf_predictive_dems)/mae_baseline_cf_dems



npat_scores_dems <- (ggplot(data = validation3, aes(x =  npat_score, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(-2, 0))
       +scale_y_continuous(limits=c(-.75,0))
     + geom_text(data = cors_cf_predictive_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.6, y =-.65)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(prospective, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_cf_predictive_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                       npat_score,use = "complete.obs"), 2))
 npat_score<-validation3$ npat_score
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric= npat_score, benchmark=dwnom1_prospective)

q2_correct_cf_predictive_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_cf_predictive_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_cf_predictive_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_cf_predictive_reps<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_cf_reps<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_cf_predictive_reps_apre<-(mae_baseline_cf_reps-mae_cf_predictive_reps)/mae_baseline_cf_reps


npat_scores_reps <- (ggplot(data = validation3, aes(x =  npat_score, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("CF Score")
      +scale_x_continuous(limits = c(0, 1.6))
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_cf_predictive_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.15, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )






###############
## Compare DW Nominate and Shor-McCarty estimates
###############

###
## Calculate ability of model to correctly classify candidates into correct party
###
prospective <- subset(analysis_2000s , !is.na(Party) & !is.na(shor_mccarty_score) & !is.na(dwnom1_prospective))
prospective <- as.data.frame(prospective)

prospective$Party2<-as.numeric(factor(prospective$Party))-1
reg<-glm(Party2~shor_mccarty_score, data=prospective, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & prospective$Party2==1]<-1
correct[predictions<.5 & prospective$Party2==0]<-1
correct[predictions<.5 & prospective$Party2==1]<-0
correct[predictions>.5 & prospective$Party2==0]<-0
sm_party_predictive<-table(correct)[2]/length(correct)
sm_observations_predictive<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###


validation3 <- subset(prospective, Party == 100 )
validation3 <- as.data.frame(validation3)

cors_sm_predictive_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      shor_mccarty_score,use = "complete.obs"), 2))


shor_mccarty_score<-validation3$shor_mccarty_score
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=shor_mccarty_score, benchmark=dwnom1_prospective)

q2_correct_sm_predictive_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_sm_predictive_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_sm_predictive_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_sm_predictive_dems<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_sm_dems<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_sm_predictive_dems_apre<-(mae_baseline_sm_dems-mae_sm_predictive_dems)/mae_baseline_sm_dems



shormccarty_dems <- (ggplot(data = validation3, aes(x = shor_mccarty_score, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Shor-McCarty Score")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,0))
    + geom_text(data = cors_sm_predictive_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.5, y =-.6)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(prospective, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_sm_predictive_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      shor_mccarty_score,use = "complete.obs"), 2))

shor_mccarty_score<-validation3$shor_mccarty_score
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=shor_mccarty_score, benchmark=dwnom1_prospective)

q2_correct_sm_predictive_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_sm_predictive_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_sm_predictive_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_sm_predictive_reps<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_sm_reps<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_sm_predictive_reps_apre<-(mae_baseline_sm_reps-mae_sm_predictive_reps)/mae_baseline_sm_reps


shormccarty_reps <- (ggplot(data = validation3, aes(x = shor_mccarty_score, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Shor-McCarty Score")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_sm_predictive_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.5, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )





###############
## Compare DW Nominate and A-M estimates
###############

prospective <- subset(analysis_2000s , !is.na(Party) & !is.na(am_estimate_tw) & !is.na(dwnom1_prospective))
prospective <- as.data.frame(prospective)

prospective$Party2<-as.numeric(factor(prospective$Party))-1
reg<-glm(Party2~am_estimate_tw, data=prospective, family="binomial")
predictions<-predict(reg, type="response")
correct<-NA
correct[predictions>.5 & prospective$Party2==1]<-1
correct[predictions<.5 & prospective$Party2==0]<-1
correct[predictions<.5 & prospective$Party2==1]<-0
correct[predictions>.5 & prospective$Party2==0]<-0
am_party_predictive<-table(correct)[1]/length(correct)
am_observations_predictive<-length(predictions)

###
## Calculate ability of model to estimate contemporaneous dw-nominate scores
###

#analysis_2000s_2010<-subset(analysis_2000s, election==2010)

validation3 <- subset(prospective, Party == 100 )
validation3 <- subset(validation3, !is.na(dwnom1_prospective) )
validation3 <- as.data.frame(validation3)

cors_am_predictive_dems <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      am_estimate_tw,use = "complete.obs"), 2))

am_estimate_tw<-validation3$am_estimate_tw
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_dems, q3=q3_dems, q4=q4_dems, metric=am_estimate_tw, benchmark=dwnom1_prospective)

q2_correct_am_predictive_dems<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_am_predictive_dems<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_am_predictive_dems<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_am_predictive_dems<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_am_dems<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_am_predictive_dems_apre<-(mae_baseline_am_dems-mae_am_predictive_dems)/mae_baseline_am_dems


am_dems <- (ggplot(data = validation3, aes(x = am_estimate_tw, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Aldrich-McKelvey Score")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(-.75,0))
      + geom_text(data = cors_am_predictive_dems, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = -.5, y =-.6)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Democrats")  
     )
      
validation3 <- subset(prospective, Party == 200 )
validation3 <- as.data.frame(validation3)

cors_am_predictive_reps <- ddply(validation3, c("Party"),  summarise, cor = round(cor(dwnom1_prospective, 
                                                      am_estimate_tw,use = "complete.obs"), 2))
                                                      
                                                      am_estimate_tw<-validation3$am_estimate_tw
dwnom1_prospective<-validation3$dwnom1_prospective
validation3<-quantile.classify(validation3,q2=q2_reps, q3=q3_reps, q4=q4_reps, metric=am_estimate_tw, benchmark=dwnom1_prospective)

q2_correct_am_predictive_reps<-table(validation3$q2== validation3$q2_predict)[2]/dim(validation3)[1]
q3_correct_am_predictive_reps<-table(validation3$q3== validation3$q3_predict)[2]/dim(validation3)[1]
q4_correct_am_predictive_reps<-table(validation3$q4== validation3$q4_predict)[2]/dim(validation3)[1]

mae_am_predictive_reps<-mean(abs(validation3$prediction-validation3$dwnom1_prospective))
mae_baseline_am_reps<-mean(abs(mean(validation3$dwnom1_prospective)-validation3$dwnom1_prospective))
mae_am_predictive_reps_apre<-(mae_baseline_am_reps-mae_am_predictive_reps)/mae_baseline_am_reps



am_reps <- (ggplot(data = validation3, aes(x = am_estimate_tw, y =
                                         dwnom1_prospective))
      + theme_bw()
    ##  + geom_text(size = 4)
     + geom_point()
 + stat_smooth(method = "lm", formula = y ~ poly(x, 2), size = 1, se=F,colour="grey")
      ##+ facet_wrap(~Year, ncol = 2)
      + ylab("DW-Nominate Score")
      + xlab("Aldrich-McKelvey Score")
      +scale_x_continuous()
       +scale_y_continuous(limits=c(0,1))
      + geom_text(data = cors_am_predictive_reps, aes(label = paste("R^2 = ", round(cor^2, 2), sep =
                                                     "")), size = 6, x
                  = 1.2, y =.1)
      +theme(plot.title = element_text(size = 20), axis.title.y =
                 element_text(size = 10), axis.title.x =
                     element_text(size = 10))
  #   + ggtitle("Republicans")  
     )



## ## ## ## 
## Figure 2
## ## ## ## 

pdf(file = paste("Figure2a.pdf", sep = ""),
    width = 8, height = 4)

multiplot( cfscores_dems,shormccarty_dems,am_dems, cols = 2)
dev.off()

pdf(file = paste("Figure2b.pdf", sep = ""),
    width = 8, height = 4)

multiplot( cfscores_reps,shormccarty_reps,am_reps,js_reps, cols = 2)
dev.off()

